/*
@Time : 2019/5/28 0028 11:06
@Author : visons
*/
package proto

import (
	"cellent_test/proto/code"
	"cellent_test/proto/msg"
	"cellent_test/proto/msgid"
	"cellent_test/svc/player_svc/player_manager"
	"github.com/davyxu/cellnet"
	"github.com/davyxu/cellnet/codec"
	"reflect"
)

var (
	Handler_LoginREQ       = func(ev cellnet.Event) { panic("C2SLogin not handler") }
	Handler_C2SPlayerAuth  = func(ev cellnet.Event) { panic("C2SPlayerAuth not handler") }
	Handler_GameHanlderREQ = func(ev cellnet.Event) { panic("not handler") }
)

func GetMessageHandler() cellnet.EventCallback {
	return func(ev cellnet.Event) {
		switch ev.Message().(type) {
		case *msg.LoginREQ:
			Handler_LoginREQ(ev)
		case *msg.PlayerAuthREQ:
			Handler_C2SPlayerAuth(ev)
		case *msg.GameHandlerREQ:
			sessionId := ev.Session().ID()
			manager := player_manager.GetPlayerManager()
			player := manager.GetPlayerBySessionId(sessionId)
			if player == nil {
				ev.Session().Send(msg.GameHandlerACK{
					Code: code.ResultCode_PlayerNotLogin,
				})
			} else {
				Handler_GameHanlderREQ(ev)
			}

		}
	}
}

func init() {
	cellnet.RegisterMessageMeta(&cellnet.MessageMeta{
		Codec: codec.MustGetCodec("gogopb"),
		Type:  reflect.TypeOf((*msg.PingREQ)(nil)).Elem(),
		ID:    int(msgid.MsgId_PingREQ),
	})
	cellnet.RegisterMessageMeta(&cellnet.MessageMeta{
		Codec: codec.MustGetCodec("gogopb"),
		Type:  reflect.TypeOf((*msg.PingACk)(nil)).Elem(),
		ID:    int(msgid.MsgId_PingACK),
	})
	cellnet.RegisterMessageMeta(&cellnet.MessageMeta{
		Codec: codec.MustGetCodec("gogopb"),
		Type:  reflect.TypeOf((*msg.LoginREQ)(nil)).Elem(),
		ID:    int(msgid.MsgId_LoginREQ),
	})
	cellnet.RegisterMessageMeta(&cellnet.MessageMeta{
		Codec: codec.MustGetCodec("gogopb"),
		Type:  reflect.TypeOf((*msg.LoginACK)(nil)).Elem(),
		ID:    int(msgid.MsgId_LoginACk),
	})

	cellnet.RegisterMessageMeta(&cellnet.MessageMeta{
		Codec: codec.MustGetCodec("gogopb"),
		Type:  reflect.TypeOf((*msg.GameHandlerREQ)(nil)).Elem(),
		ID:    int(msgid.MsgId_GameHandlerREQ),
	})
	cellnet.RegisterMessageMeta(&cellnet.MessageMeta{
		Codec: codec.MustGetCodec("gogopb"),
		Type:  reflect.TypeOf((*msg.GameHandlerACK)(nil)).Elem(),
		ID:    int(msgid.MsgId_GameHandlerACk),
	})

}
